/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * alertGuardar.java
 *
 * Created on 04-may-2009, 10:45:51
 */

package alertas;
import gestion.Trabajo;
import clientes.Doctor;
import productos.Producto;
import clientes.Cliente;
import gestion.Factura;
import laboratorio.*;
import java.util.*;

import laboratorio.*;

/**
 *
 * @author Juan Antonio Tirado
 */
public class alertGuardar extends javax.swing.JFrame {
    //ATRIBUTOS
    private Productos VentanaProductoOld;
    private Clientes VentanaClienteOld;
    private Doctores VentanaDoctorOld;
    private Trabajos VentanaTrabajoOld;
    private Facturas VentanaFacturaOld;
    private String tipo;

    /** Creates new form alertGuardar */
    public alertGuardar() {
        initComponents();
    }
    public alertGuardar(Productos productosOld) {
        initComponents();
        VentanaProductoOld=productosOld;
        tipo="productos";
    }
    public alertGuardar(Clientes clientesOld) {
        initComponents();
        VentanaClienteOld=clientesOld;
        tipo="clientes";
    }
    public alertGuardar(Doctores doctoresOld) {
        initComponents();
        VentanaDoctorOld=doctoresOld;
        tipo="doctores";
    }
    public alertGuardar(Trabajos trabajosOld) {
        initComponents();
        VentanaTrabajoOld=trabajosOld;
        tipo="trabajos";
    }
    public alertGuardar(Facturas facturasOld) {
        initComponents();
        VentanaFacturaOld=facturasOld;
        tipo="facturas";
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lpdPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("lpdPU").createEntityManager();
        clienteQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT c FROM Cliente c");
        clienteList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : clienteQuery.getResultList();
        doctorQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT d FROM Doctor d");
        doctorList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : doctorQuery.getResultList();
        productoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT p FROM Producto p");
        productoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : productoQuery.getResultList();
        trabajoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT t FROM Trabajo t");
        trabajoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : trabajoQuery.getResultList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboratorio.LaboratorioApp.class).getContext().getResourceMap(alertGuardar.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                salir(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                GuardarSalir(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(240, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(79, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_salir
        this.setVisible(false);
        if(tipo.equals("productos")){
            VentanaProductoOld.setVisible(false);
        }else if(tipo.equals("clientes")){
            VentanaClienteOld.setVisible(false);
        }else if(tipo.equals("doctores")){
            VentanaDoctorOld.setVisible(false);
        }else if(tipo.equals("trabajos")){
            VentanaTrabajoOld.setVisible(false);
        }else if(tipo.equals("facturas")){
            VentanaFacturaOld.setVisible(false);
        }
        
    }//GEN-LAST:event_salir

    private void GuardarSalir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_GuardarSalir
        // TODO add your handling code here:
        if (tipo.equals("productos")){
            Producto p=VentanaProductoOld.CogeProducto();
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.merge(p);
            lpdPUEntityManager.getTransaction().commit();
            this.setVisible(false);
            VentanaProductoOld.setVisible(false);
        }else if(tipo.equals("doctores")){
            Doctor d=VentanaDoctorOld.CogeDoctor();
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.merge(d);
            lpdPUEntityManager.getTransaction().commit();
            this.setVisible(false);
            VentanaDoctorOld.setVisible(false);
        }else if(tipo.equals("clientes")){
            Cliente c=VentanaClienteOld.CogeCliente();
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.merge(c);
            lpdPUEntityManager.getTransaction().commit();
            this.setVisible(false);
            VentanaClienteOld.setVisible(false);
        }else if(tipo.equals("trabajos")){
            this.setVisible(false);
            VentanaTrabajoOld.setVisible(false);
        }else if(tipo.equals("facturas")){
            Factura f=VentanaFacturaOld.CogeFactura();
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.merge(f);
            lpdPUEntityManager.getTransaction().commit();
            this.setVisible(false);
            VentanaFacturaOld.setVisible(false);
        }

    }//GEN-LAST:event_GuardarSalir

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new alertGuardar().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<clientes.Cliente> clienteList;
    private javax.persistence.Query clienteQuery;
    private java.util.List<clientes.Doctor> doctorList;
    private javax.persistence.Query doctorQuery;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.persistence.EntityManager lpdPUEntityManager;
    private java.util.List<productos.Producto> productoList;
    private javax.persistence.Query productoQuery;
    private java.util.List<gestion.Trabajo> trabajoList;
    private javax.persistence.Query trabajoQuery;
    // End of variables declaration//GEN-END:variables

}
